﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Collections;

namespace CDTM.Modules.TraCuuDiem.Components
{
    public partial class TuyenSinh
    {
        public static List<TuyenSinh> SelectBy(int sbd, string hoTen)
        {
            //using (CDTMEntities entity = new CDTMEntities())
            //{
            //    IList<TuyenSinhLINQ> list = entity.TuyenSinhLINQSet.Where(x => x.SBD == sbd
            //        && (x.HoTenLot.Contains(hoTen) || x.Ten.StartsWith(hoTen))).OrderBy(x => x.Ten).ToList<TuyenSinhLINQ>();
            //}

            List<TuyenSinh> list = TuyenSinh.SelectCollectionAll().Where(x => x.SBD == sbd
               && (x.HoTenLot.Contains(hoTen) || x.Ten.StartsWith(hoTen))).OrderBy(x => x.Ten).ToList<TuyenSinh>();

            return list;

        }

        public static List<TuyenSinh> SelectBy(int sbd)
        {
            //using (CDTMEntities entity = new CDTMEntities())
            //{
            //    IList<TuyenSinhLINQ> list = entity.TuyenSinhLINQSet.Where(x => x.SBD == sbd).OrderBy(x => x.Ten).ToList<TuyenSinhLINQ>();
            //}

            List<TuyenSinh> list = TuyenSinh.SelectCollectionAll().Where(x => x.SBD == sbd).OrderBy(x => x.Ten).ToList<TuyenSinh>();

            return list;

        }

        public static List<TuyenSinh> SelectBy(string hoTen)
        {
            //using (CDTMEntities entity = new CDTMEntities())
            //{
            //    IList<TuyenSinhLINQ> list = entity.TuyenSinhLINQSet.Where(x => x.HoTenLot.Contains(hoTen) || x.Ten.StartsWith(hoTen)).OrderBy(x => x.Ten).ToList<TuyenSinhLINQ>();
            //}

            List<TuyenSinh> list = TuyenSinh.SelectCollectionAll().Where(x => x.HoTenLot.Contains(hoTen) || x.Ten.StartsWith(hoTen)).OrderBy(x => x.Ten).ToList<TuyenSinh>();

            return list;

        }

        public static List<TuyenSinh> Clean(List<TuyenSinh> list)
        {
            List<TuyenSinh> newList = new List<TuyenSinh>();

            var removeList = from item in list where item.SBD != 0 select item;

            foreach (TuyenSinh obj in removeList)
            {
                newList.Add(obj);
            }

            return newList;
        }

        //---------------------------------------------------------------------------------------------

        public static int DeleteAll()
        {
            return DeleteDynamic("1=1");
        }

        //---------------------------------------------------------------------------------------------

        public static int DeleteDynamic(string whereCondition)
        {
            const string spName = "[dbo].[p_CDTM_TCD_TuyenSinh_DeleteDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.Text, whereCondition);

            return db.ExecuteNonQuery(dbCommand);
        }

    }
}
